package today.rocky.boot.shiro.realm;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import today.rocky.boot.shiro.entity.User;
import today.rocky.boot.shiro.service.UserService;

/**
 * @author Rocky
 * @version 1.0
 * @date 2020/4/14 13:21
 */
@Slf4j
public class UserRealm extends AuthorizingRealm {

	@Autowired
	private UserService userSerivce;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		log.info("执行授权逻辑");
		AuthorizationInfo info = new SimpleAuthorizationInfo();

		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
		log.info("执行认证逻辑");
		//编写shiro判断逻辑，判断用户名和密码
		//1.判断用户名
		UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
		User user = userSerivce.findByName(token.getUsername());
		if(user==null){
			//用户名不存在
			//shiro底层会抛出UnKnowAccountException
			return null;
		}

		//2.判断密码
		return new SimpleAuthenticationInfo("",user.getPassword(),"");

	}
}
